﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BProdSkuProduct
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/9 8:33:13
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
//using MyBatis.DataMapper.TypeHandlers.Nullables;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BWcsScanner : IWcsScanner
{
    // 创建成功
    public int AddWcsScanner(long scannerLocId, string productCode, int scannerCount, string remark, int priority)
    {
        var db = Db.Default;
        var result = 0;
        //保证同一条码只插入一条 除了noread
        if (productCode.StartsWith("NOREAD"))
        {
            result = db.WcsScanners.Insert(new POWcsScanner
            {
                Id = XID.Nextval,
                StartLoc = scannerLocId,
                Barcode = productCode,
                RemarkMemo = remark,
                Priority = priority,
                ScannerCount = scannerCount,
                //HeightType = heightType,
            });
        }
        else
        {

            //var isHave = db.WcsScanners.Where(t => t.StartLoc == scannerLocId)
            //    .Take(4).OrderByDescending(t => t.CreateTime).Where(t => t.Barcode == productCode).FirstOrDefault();

            //if (isHave is not null)
            //{
            //    return result;
            //}

            //if (scannerCount == 0)
            //{
            //    result = db.WcsScanners.Insert(new POWcsScanner
            //    {
            //        Id = XID.Nextval,
            //        StartLoc = scannerLocId,
            //        Barcode = productCode,
            //        RemarkMemo = remark,
            //        Priority = priority,
            //        ScannerCount = scannerCount,
            //        //HeightType = heightType,
            //    });
            //}
            //else
            //{

            ////没有条码就插入 有相同条码 就把use_flag改成0 然后再插入
            if (db.WcsScanners.Where(t => t.Barcode == productCode && t.UseFlag == 1).FirstOrDefault() is null)
            {
                result = db.WcsScanners.Insert(new POWcsScanner
                {
                    Id = XID.Nextval,
                    StartLoc = scannerLocId,
                    Barcode = productCode,
                    RemarkMemo = remark,
                    Priority = priority,
                    ScannerCount = scannerCount,
                    //HeightType = heightType,
                });
            }
            else
            {
                ////只有已经生成任务的才修改并重新插入
                //if (db.WcsScanners.Where(t => t.Barcode == productCode && t.UseFlag == 1 && t.Status == 2).FirstOrDefault() is not null)
                //{
                ////如果是下传口来的 如果有扫描记录 则不变 
                //if (priority == 0)
                //{

                var scanners = db.WcsScanners.Where(t => t.Barcode == productCode && t.UseFlag == 1).ToList();
                foreach (var item in scanners)
                {
                    item.UseFlag = 0;
                }
                db.WcsScanners.UpdateRange(scanners);
                db.SaveChanges();
                result = db.WcsScanners.Insert(new POWcsScanner
                {
                    Id = XID.Nextval,
                    StartLoc = scannerLocId,
                    Barcode = productCode,
                    RemarkMemo = remark,
                    Priority = priority,
                    ScannerCount = scannerCount,
                    //HeightType = heightType,
                });
                //}
            }

            //}
            //}
        }
        return result;
    }


    public POWcsScanner GetFirstNoTaskWcsScannerByStartLoc(long startLoc)
    {
        var db = Db.Default;
        var result = db.WcsScanners.Where(t => t.Status == (int)EnumScannerStatus.NoCreate && t.UseFlag == 1 && t.StartLoc == startLoc && t.CreateTime >= DateTime.Now.AddDays(-1))
            .OrderByDescending(t => t.Priority)
            .ThenBy(t => t.CreateTime).FirstOrDefault();
        return result;
    }
    public POWcsScanner GetWcsScannerByProduct(long startLoc, string productCode)
    {
        var db = Db.Default;
        var result = db.WcsScanners.Where(t => t.StartLoc == startLoc && t.Barcode == productCode && t.CreateTime >= DateTime.Now.AddDays(-1))
            .OrderByDescending(t => t.CreateTime).FirstOrDefault();
        return result;
    }

    public int UpdateWcsScannerErrorById(long id, long errorCode)
    {
        var db = Db.Default;
        var result = 0;
        var scanner = db.WcsScanners
        .Where(t => t.Id == id).FirstOrDefault();
        scanner.ErrorTime = DateTime.Now;
        scanner.ErrorCode = errorCode;
        result = db.SaveChanges();
        return result;
    }

    public int UpdateWcsScannerStatusById(long id, int status, string remarkMemo, long taskId = 0)
    {
        var db = Db.Default;
        var result = 0;
        if (taskId <= 0)
        {
            result = db.WcsScanners
            .Where(t => t.Id == id)
            .Update(t => new()
            {
                Status = status,
                StatusTime = DateTime.Now,
                RemarkMemo = remarkMemo,
                UpdateTime = DateTime.Now,
                TaskId = taskId,
            });
        }
        else
        {
            result = db.WcsScanners
            .Where(t => t.Id == id)
            .Update(new POWcsScanner()
            {
                Status = status,
                RemarkMemo = remarkMemo,
                SuccessTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                TaskId = taskId,
            });
        }

        return result;
    }

    public int DeleWcsScannerByTaskId(long taskId, string remark)
    {
        var db = Db.Default;
        var result = 0;
        var scanners = db.WcsScanners.Where(t => t.TaskId == taskId).ToList();

        foreach (var item in scanners)
        {
            item.UseFlag = 0;
            item.UpdateTime = DateTime.Now;
            item.FinishTime = DateTime.Now;
            item.RemarkMemo = remark;

        }
        db.WcsScanners.UpdateRange(scanners);
        result = db.SaveChanges();
        return result;
    }



    public int InsertWcsScannerAbnormal(long locId, string remark, string productCode)
    {
        var db = Db.Default;

        var result = db.WcsScannerAbnormals.Insert(new POWcsScannerAbnormal
        {
            Id = XID.Nextval,
            LocId = locId,
            Remark = remark,
            Barcode = productCode,
            CreateTime = DateTime.Now,
        });
        return result;

    }

}
